#!/usr/bin/python

import select
import socket
import time
import sys
import struct

from impacket import ImpactDecoder, ImpactPacket

if len(sys.argv) < 2:
	print "Use: %s <dst ip>" % sys.argv[0]
	sys.exit(1)

dst = sys.argv[1]

# Create a new IP packet and set its source and destination addresses.

ip = ImpactPacket.IP()
ip.set_ip_dst(dst)

icmp = ImpactPacket.ICMP()
# 15 == REQUEST INFORMATION"
icmp.set_icmp_type(15)

# Include ra as payload inside the ICMP packet.
icmp.contains(ImpactPacket.Data("ra70168"))

# Have the IP packet contain the ICMP packet (along with its payload).
ip.contains(icmp)

# Open a raw socket. Special permissions are usually required.
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

icmp.set_icmp_id(0)

# Calculate its checksum.
icmp.set_icmp_cksum(0)
icmp.auto_checksum = 1

# Send it to the target host.
s.sendto(ip.get_packet(), (dst, 0))

# Wait for incoming replies.
if s in select.select([s],[],[],1)[0]:
	reply = s.recvfrom(2000)[0]
	
	# Use ImpactDecoder to reconstruct the packet hierarchy.
	rip = ImpactDecoder.IPDecoder().decode(reply)
	# Extract the ICMP packet from its container (the IP packet).
	ricmp = rip.child()

	# 16 == information reply
	print "ICMP reply: %s" % ricmp.child()
